package com.glorystone.ins.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.glorystone.ins.common.JsonResult;
import com.glorystone.ins.common.common.DataTable;
import com.glorystone.ins.common.common.PageVo;
import com.glorystone.ins.domain.SysAuth;
import com.glorystone.ins.interceptor.PageList;
import com.glorystone.ins.service.ISysAuthService;

/**
 * 权限controller
 *
 */
@Controller
@RequestMapping("/sysauth")
public class SysAuthController extends BaseController {

    @Autowired
    private ISysAuthService sysAuthService;

    /**
     * 去往权限管理列表
     */
    @RequestMapping("/tolist")
    public ModelAndView tolist(ModelAndView mv){
        mv.setViewName("system/sysauth_list");
        return mv;
    }

    /**
     * 组装权限列表数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/queryTableData",method = RequestMethod.GET)
    @ResponseBody
    public DataTable<SysAuth> queryTableData(HttpServletRequest request) {
        PageVo vo =  parametToPageVo(request,SysAuth.class);
        PageList<SysAuth> list = sysAuthService.queryAuthList(vo);
        DataTable data = resultToDataTable(list);
        return data;
    }


    /**
     * 去往新增-权限
     */
    @RequestMapping("/toadd/{type}")
    public ModelAndView toAdd(@PathVariable String type, ModelAndView mv){
        List<SysAuth> list = new ArrayList<SysAuth>();
        if(type.equals("menu")){
            list = sysAuthService.getRootMenu();
        }else{
            list = sysAuthService.getChildMenu();
        }
        mv.addObject("type", type);
        mv.addObject("list", list);
        mv.setViewName("system/sysauth_add");
        return mv;
    }

    /**
     * 去往编辑-权限
     */
    @RequestMapping("/toedit/{tid}")
    public ModelAndView toEdit(@PathVariable Long tid, ModelAndView mv){
        //组装父级资源数据
        SysAuth sysAuth = sysAuthService.getAuthById(tid);

        if(sysAuth.getParentId()==0){//顶级菜单
            mv.addObject("parentName", "根级菜单");
        }else{
            SysAuth parentAuth = sysAuthService.getAuthById(sysAuth.getParentId());
            mv.addObject("parentName", parentAuth.getText());

        }

        mv.addObject("type", sysAuth.getIconcls());
        mv.addObject("auth", sysAuth);

        mv.setViewName("system/sysauth_add");
        return mv;
    }

    /**
     * 添加/编辑-权限
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addAuth(SysAuth sysAuth) {
        if(sysAuth.getTid() != null && sysAuth.getTid() > 0){
            sysAuthService.updateAuth(sysAuth);
        }else{
            sysAuthService.addAuth(sysAuth);
        }
        return "redirect:/sysauth/tolist";
    }

    /**
     * 删除权限
     */
    @RequestMapping("/del/{id}")
    public @ResponseBody JsonResult delAuth(@PathVariable Long id){
        boolean result = sysAuthService.delAuth(id);
        if(result){
            return returnSuccess("操作成功！");
        }else{
            return JsonResult.createErrorMsg("操作失败！");
        }
    }
	 

}
